package com.fz.us.oa.web.controller.com;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.dict.entity.Dict;
import com.fz.us.admin.dict.entity.DictItem;
import com.fz.us.base.bean.BaseEnum;
import com.fz.us.oa.core.entity.com.Resident;
import com.fz.us.oa.core.service.com.ResidentService;
import com.fz.us.oa.web.controller.BaseController;
import com.fz.us.oa.web.shiro.bind.annotation.CurrentUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dzw on 2018/3/13.
 * 居民区系统字典
 */
@Controller
@RequestMapping("/com/resident")
public class ResidentController extends BaseController {
    private static final long serialVersionUID = 250818159942243688L;

    @Autowired
    private ResidentService residentService;

    /**
     * 页面
     */
    @RequestMapping(value = "toList", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String toList(@CurrentUser User user, Model model){
        Dict dict = dictService.getByValue("street");
        if(dict!=null){
            List<DictItem> dictItemList = dictItemService.listSystem(dict.getId());
            model.addAttribute("streetList", dictItemList);
        }

        return "/main/menu/com/resident";
    }

    /**
     * 根据街道获取居民区列表
     */
    @RequestMapping(value = "getResidentList", method = RequestMethod.GET)
    @ResponseBody
    public Object getResidentList(@CurrentUser User user, String parentId){
        DictItem streetDict = dictItemService.get(parentId);

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("state", BaseEnum.StateEnum.Enable);
        params.put("company", user.getCompany());
        params.put("street", streetDict);
        List<Resident> residentList = residentService.getList(params);

        Map<String, Object> data = new HashMap<String, Object>();
        List<Map<String, Object>> dataRows = new ArrayList<Map<String, Object>>();
        Map<String, Object> rMap;

        for(Resident resident:residentList){
            rMap = new HashMap<String, Object>();
            rMap.put("id", resident.getId());
            rMap.put("name", StringUtils.isNotEmpty(resident.getName()) ? resident.getName() : "");
            dataRows.add(rMap);
        }
        data.put("data", dataRows);

        return data;
    }

    /**
     * 进入dialog
     */
    @RequestMapping(value = "residentDialog", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String residentDialog(Model model, String parentId, String text, String keyId){
        model.addAttribute("parentId", parentId);
        model.addAttribute("text", text);
        if(StringUtils.isNotEmpty(keyId)){
            Resident resident = residentService.get(keyId);
            model.addAttribute("keyId", keyId);
            model.addAttribute("name", resident.getName());
        }
        return "/com/resident_dialog";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "saveResident", method = RequestMethod.POST)
    public Object saveResident(@CurrentUser User user, String parentId, String name, String keyId){
        if(StringUtils.isNotEmpty(keyId)){
            Resident resident = residentService.get(keyId);
            resident.setName(name);
            residentService.update(resident);
            return ajaxJson(resultService.success("修改成功"));
        }else{
            if(StringUtils.isNotEmpty(parentId) && StringUtils.isNotEmpty(name)){
                Resident resident = new Resident();
                resident.setCompany(user.getCompany());
                resident.setCreater(user);
                resident.setName(name);
                resident.setStreet(dictItemService.get(parentId));
                residentService.save(resident);
                return ajaxJson(resultService.success("保存成功"));
            }else{
                return ajaxJson(resultService.fail("保存失败"));
            }
        }
    }

    /**
     * 删除字典
     */
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public Object delete(@CurrentUser User user, String keyId){
        if(StringUtils.isNotEmpty(keyId)){
            Resident resident = residentService.get(keyId);
            resident.setState(BaseEnumManager.StateEnum.Delete);
            residentService.update(resident);
        }
        return ajaxJson(resultService.success("删除成功"));
    }
}
